import { useLocation } from "umi";
import { request } from "umi";
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { LoginForm, ProFormText, } from '@ant-design/pro-components';
import { Alert, Form, message, Spin } from 'antd';
import React, { useEffect, useState } from 'react';
import { history, useIntl, useModel } from 'umi';
import styles from './index.less';
import { getCurrentUserMenu, login } from '@/services/api/user';
export default () => {
    const location = useLocation()
    const { initialState, setInitialState } = useModel('@@initialState');
    const fetchUserInfo = async (userInfo: any) => {
        console.log(userInfo);
        const userCurrentMenu: any = await getCurrentUserMenu({});
        console.log(userCurrentMenu);
        window.currentMenus = userCurrentMenu;
        let menusKeys = {}
        userCurrentMenu.data.map((e: any, i: number) => {
            menusKeys[e.menuKey] = e
        });
        window.currentMenus = menusKeys
        let hasMenus: any = [];
        let currentMenus = userCurrentMenu?.data;
        let buttonAuth = {};
        currentMenus.map((e: any, i: number) => {
            hasMenus.push(e.menuKey);
        });
        // });
        await new Promise<void>((resolve, reject) => {
            setTimeout(() => {
                resolve()
            }, 300);
        })

        await setInitialState((s) => ({
            ...s,
            currentUser: userInfo,
            userMenusKeys: hasMenus,
            accessData: []
        }));
    };
    const getUserInfo = async (code) => {
        try {
            console.log(code);
            // 登录
            // const msg: any = await login({
            //   ...values,
            // });
            // console.log(msg);
            debugger
            request('/login/auth/callback?code=' + code, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                },
                data: {

                },
            }).then(async (res) => {
              debugger
                if (res?.data?.result_flag) {
                    message.success('登录成功！', 1);
                    await fetchUserInfo(res.data?.data);
                    debugger
                    /** 此方法会跳转到 redirect 参数所在的位置 */
                    history.push('/admin/home');
                    return;
                }
            }).catch((err) => {
              console.log(err);
            })
        } catch (error) {
            console.log(error);
        }
    }
    useEffect(() => {
        debugger
        console.log(location.query)
        getUserInfo(location.query.code)
        // `http://iamtest.szgas.com:8080/idp/oauth2/authorize?redirect_uri=${encodeURIComponent('https://cockpit.szgas.com.cn/auth')}&state=login&client_id=SRHY&response_type=code`
    }, [])
    return <Spin />;
}
